package com.tequila.core.service.impl;

import com.google.common.collect.Lists;
import com.tequila.core.dao.FileDao;
import com.tequila.core.service.FileService;
import com.tequila.domain.dto.FileDto;
import com.tequila.domain.entity.File;
import com.tequila.domain.query.FileQuery;
import fe.core.Assert;
import fe.core.BeanHelper;
import fe.core.Pagination;
import fe.core.aliyun.AliyunOssFileService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class FileServiceImpl implements FileService {
    public static final Logger logger = LoggerFactory.getLogger(FileServiceImpl.class);

    @Resource
    private FileDao fileDao;

    @Resource
    private AliyunOssFileService aliyunOssFileService;


    @Override
    public Pagination<FileDto> queryPageListByParam(FileQuery query) {
        Assert.assertNotNull(query);
        int total = fileDao.queryPageCountByParam(query);

        List<FileDto> list = Lists.newArrayList();
        if (total > 0) {
            list = fileDao.queryPageListByParam(query);
        }
        return new Pagination<>(query, list, total);
    }

    @Override
    public FileDto queryById(Long id) {
        Assert.assertNotNull(id,"id不能为空!");
        File file = fileDao.queryById(id);
        FileDto fileDto = new FileDto();
        BeanHelper.copyProperties(fileDto,file);
        return fileDto;
    }

    @Override
    public List<FileDto> queryByIdList(List<Long> idList) {
        Assert.assertNotNull(idList,"idList不能为空!");
        return fileDao.queryByIdList(idList);
    }

    @Override
    public List<FileDto> queryListByParam(FileQuery query) {
        Assert.assertNotNull(query,"query不能为空!");
        return fileDao.queryListByParam(query);
    }


    @Override
    public Long insert(File entity) {
        Assert.assertNotNull(entity,"entity不能为空!");
        fileDao.insert(entity);
        return entity.getId();
    }

    @Override
    public Long save(File entity) {
        Assert.assertNotNull(entity,"entity不能为空!");
        fileDao.insertSelective(entity);
        return entity.getId();
    }

    @Override
    public int updateById(File param) {
        Assert.assertNotNull(param,"param不能为空!");
        Assert.assertNotNull(param.getId(),"id不能为空!");
        return fileDao.updateById(param);
    }

    @Override
    public int updateSelectiveById(File param) {
        Assert.assertNotNull(param,"param不能为空!");
        Assert.assertNotNull(param.getId(),"id不能为空!");
        return fileDao.updateSelectiveById(param);
    }

    @Override
    public int deleteById(Long id) {
        Assert.assertNotNull(id,"id不能为空!");
        return fileDao.deleteById(id);
    }

    @Override
    public int deleteByIdList(List<Long> idList) {
        Assert.assertNotNull(idList,"idList不能为空!");
        return fileDao.deleteByIdList(idList);
    }


    public int saveFile(File file) {
        return fileDao.insertSelective(file);
    }

    public List<String> convertFileIdList(String fileIdListStr, Map<Long, File> fileMap) {
        List<String> picFileUrlList = Lists.newArrayList();
        if (StringUtils.isNotEmpty(fileIdListStr)) {
            String[] strs = fileIdListStr.split(",");
            for (String str : strs) {
                File f = fileMap.get(Long.parseLong(str));
                if (f != null) {
                    String fileUrl = aliyunOssFileService.getFileUrl(f.getBucket(), f.getLocation(), f.getUnqFileName());
                    picFileUrlList.add(fileUrl);
                }
            }
        }
        return picFileUrlList;
    }
}
